<!-- 字典管理 -->
<template>
  <div style="padding-left:20px;">
    <el-form :inline="true" class="demo-form-inline">
      <el-form-item>
        <el-form-item>
          <el-input v-model="data.name" size="medium" class="iup_width200" placeholder="请输入名称"></el-input>
        </el-form-item>
        <el-form-item>
          <el-input v-model="data.type" size="medium" class="iup_width200" placeholder="请输入类型"></el-input>
        </el-form-item>

        <el-form-item>
          <el-input v-model="data.typeName" size="medium" class="iup_width200" placeholder="请输入类型名称"></el-input>
        </el-form-item>

      </el-form-item>
      <el-form-item>
        <el-button type="primary" size="medium" @click="search">查询</el-button>
        <el-button type="info" size="medium" @click="reset">重置</el-button>
      </el-form-item>
      <el-form-item class="flr">
        <el-button type="primary" size="medium" @click="add('create','')">添加</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="getList" v-loading="loading" element-loading-text="数据加载中" empty-text border stripe>
      <div slot="empty">
        <div v-if="loading"></div>
        <div v-else>暂无数据</div>
      </div>
      <el-table-column type="index" label="序号" align="center" width="70"></el-table-column>
      <el-table-column prop="code" label="代号" align="center"></el-table-column>
      <el-table-column prop="name" label="名称" align="center"></el-table-column>
      <el-table-column prop="sort" label="排序" align="center"></el-table-column>
      <el-table-column prop="type" label="类型" align="center"></el-table-column>
      <el-table-column prop="typeName" label="类型名称" align="center"></el-table-column>

      <el-table-column label="操作" width="200" align="center">
        <template slot-scope="scope">
          <el-button type="primary" size="small" @click="add('edit',scope.row)">修改</el-button>
          <el-button type="danger" size="small" @click="del(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页开始 -->
    <pagination :total="total" :pageData="data" @hanleSize="changePage" @hanlePage="handleSizeChange"></pagination>
    <!-- 分页结束 -->

    <!-- 字典表添加/删除 -->
    <DictListModal v-if="dictData.showModal" :dictData="dictData" @close="getRoleList" />

  </div>
</template>
<script>
import DictListModal from 'component/permissions/DictListModal'
export default {
  components: {
    DictListModal
  },
  data () {
    return {
      getList: [],
      data: {
        pageSize: 10,
        currentPage: 1,
        typeName: '', // 类型类型
        type: '', // 类型
        name: '', // 名称
      },
      total: 0,
      loading: false,
      getTime: '',
      dictData: {},
    }
  },
  methods: {
    //点击页数触发
    changePage (page) {
      this.data.currentPage = page
      this.getRoleList();
    },
    //选择页数最多显示几条数据
    handleSizeChange (val) {
      this.data.pageSize = val
      this.getRoleList()
    },
    //查询
    changeInput () {
      this.data.currentPage = 1
      this.getTime();
    },
    //查询关键字
    search () {
      this.data.currentPage = 1
      this.getRoleList();
    },
    //重置列表
    reset () {
      Object.assign(this.$data.data, this.$options.data().data)//重置列表
      this.getRoleList();
    },
    //获取列表
    async getRoleList () {
      this.loading = true
      let url = '/dict/queryPage'
      let result = await this.request.httpRequest('POST', url, this.data)
      if (result.code == 200) {
        this.total = result.data.totalRows
        this.data.pageSize = result.data.pageSize
        this.data.currentPage = result.data.currentPage
        this.getList = result.data.datas
      } else {
        this.$message({ type: 'error', message: result.msg })
      }
      this.loading = false
    },
    //是否要删除
    del (row) {
      this.$confirm(`您确定要删除此字典吗？`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        this.loading = true
        let url = `/dict/delete/${row.id}`
        let result = await this.request.httpRequest('DELETE', url)
        if (result.code == 200) {
          this.$message({ type: 'success', message: result.msg });
          this.getRoleList();
        } else {
          this.loading = false
          this.$message({ type: 'error', message: result.msg })
        }
      })
    },
    //新增、修改
    add (type, row) {
      this.dictData = {
        title: type == 'create' ? '添加字典' : '修改字典',
        showModal: true,
        roleId: row.id || '',
        type: type
      }
    },


  },
  mounted () {
    this.getRoleList();
    this.getTime = this.helper.debounce(this.getRoleList)//防抖
  },
  created () {
    document.onkeydown = (e) => {
      if (e.keyCode == 13) {
        this.search();
      }
    };
  }
}
</script>
<style scoped>
.el-form {
  padding: 15px 0px !important;
}
.el-form-item {
  margin-bottom: 0px !important;
}
</style>